/*
 * FileSys.h
 *
 *  Created on: Jul 9, 2009
 *      Author: oferbart
 */

#ifndef FILESYS_H_
#define FILESYS_H_

#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include "Utils.h"
#include "MetadataBlock.h"
#include "INode.h"
#include "BlockNode.h"
#include "Utils.h"
#include "Log.h"
#include "HD.h"
#include "Directory.h"
#include <utility>
#include <math.h>
using namespace std;

class FileSys {

private:
	HD* _hd;
	vector<int>* getBlocksNumListByOffset(int i_node, int offset, int nBytes);

public:
	FileSys(HD* hd);
	virtual ~FileSys();

	// creates a file returning its i-node number. Flag indicates whether the file to create is a regular file or a soft link.
	int createFile(int flag);

	// creates a directory, returning its i-node number.
	int createDir();

	// Returns the type of the file this i-node represents.
	int getFileType(int i_node);

	// reads nBytes (at most) from the given file (represented by this i-node) at the given offset inside the file into the buffer.
	int f_read(int i_node, unsigned char* buffer, int offset, int nBytes);

	/* writes nBytes (at most) to the given file (represented by this i-node) at the given offset inside that file, from the buffer.
	If the amount to be written exceeds the current allocated block, a new block should be allocated to accommodate the rest of the data.
	Notice that both f_read and f_write only work on normal files (not directories).*/
	int f_write(int i_node, unsigned char* buffer, int offset, int nBytes);

	/* reads the entire content of the directory file specified by the given i-node. This information is then entered into a data structure
	(a vector or list) that lists the files in this directory. Each element in this list therefore contains a filename and i-node number.*/
	vector<pair<int,string> >* d_read(int i_node);

	/* replaces the content of the given directory file (specified by i-node) with the information stored in the list.
	The list has the same format as that returned by d_read().*/
	int d_write(int i_node, vector<pair<int,string> >* list);

	// delete the file.
	int f_delete(int i_node);

	// delete the directory if it is empty.
	int d_delete(int i_node);

	int getBlockSizeInBytes();

	int getRootinodeNum();

	int getFileSize(int i_node);

	int incHardLinks(int i_node);

	int decHardLinks(int i_node);


};

#endif /* FILESYS_H_ */
